/**
 *
 */
package com.zhengtoon.mctoon.conv.service;

import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.bean.ConvHandleRecord;
import com.zhengtoon.mctoon.conv.bean.ConvPhotoInfo;
import com.zhengtoon.mctoon.conv.dao.ConvHandleRecordMapper;
import com.zhengtoon.mctoon.conv.dao.ConvPhotoInfoMapper;
import com.zhengtoon.mctoon.conv.dto.HandleRecordListResultItemDto;
import com.zhengtoon.mctoon.conv.dto.mybatis.Page;
import com.zhengtoon.mctoon.conv.exception.ConvPhotoExceptionCode;
import com.zhengtoon.mctoon.conv.service.ConvHandleRecordService;
import com.zhengtoon.mctoon.conv.utils.AppConstant;
import com.zhengtoon.mctoon.conv.utils.DateFormatAppUtils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理 impl实现
 *
 * @author 赵康峰
 * @since 2017-05-26
 * Copyright: Copyright (c) 2017
 * Company:北京政务通科技有限公司
 */
@Service
public class ConvHandleRecordServiceImpl implements ConvHandleRecordService {

    private static Logger log = LoggerFactory.getLogger(ConvHandleRecordServiceImpl.class);

    @Autowired
    private ConvHandleRecordMapper convHandleRecordMapper;

    @Autowired
    private ConvPhotoInfoMapper convPhotoInfoMapper;

    @Override
    public Page queryHandleHistoryById(Long convId, Page page) {
        try {
            // 1.查询状态为举报的随后拍记录, 未锁定的

            // 2.分页,条件查询

            // 查询数据
            List<ConvHandleRecord> handleRecordList = convHandleRecordMapper.queryHandleHistoryById(page);

            // 查询数据条数
            Integer dataTotal = convHandleRecordMapper.queryHandleHistoryByIdCount(page);

            //  参数转换,处理
            List<HandleRecordListResultItemDto> resultItmeDtoList = convertToHandleRecordListResultItemDto(convId, handleRecordList);


            // 返回参数
            if (dataTotal == null) {
                dataTotal = 0;
            }

            page.setResult(resultItmeDtoList);
            page.setDataTotal(dataTotal);

            return page;
        } catch (Exception e) {
            log.error("查看处理历史", e);
            throw new BusinessException(ConvPhotoExceptionCode.QUERY_HANDLE_RECORD_ERROR);
        }
    }

    /**
     * 实体转换为dto对象
     *
     * @param convId           随手拍记录id
     * @param handleRecordList 处理历史记录实体list
     * @return List<HandleRecordListResultItemDto>
     * @author 144637
     */
    private List<HandleRecordListResultItemDto> convertToHandleRecordListResultItemDto(Long convId, List<ConvHandleRecord> handleRecordList) {

        List<HandleRecordListResultItemDto> dtoList = new ArrayList<>();
        // 查询是否是提交到首都之窗
        ConvPhotoInfo convEntity = convPhotoInfoMapper.queryConvDetailById(convId);
        for (ConvHandleRecord item : handleRecordList) {
            HandleRecordListResultItemDto dto = new HandleRecordListResultItemDto();
            dto.setId(item.getHandleRecordId());
            dto.setHandlePerson(item.getHandlePerson());
            dto.setHandleTime(DateFormatAppUtils.getFormatDate(item.getCreateTime()));
            dto.setHandleWay(item.getHandleMode());

            String handleChannel = item.getHandleChannel();
            String handleResult = item.getHandleResult();

            // 如果提交到首都之窗,而且处理途径和处理结果为空
            if (AppConstant.HANDLE_MODEL_BJ_WINDOW.equals(convEntity.getHandleType())) {
                if (StringUtils.isAnyBlank(item.getHandleChannel(), item.getHandleResult())) {
                    handleChannel = AppConstant.BJ_WINDOW_HANDLE_CHANNEL;
                    handleResult = AppConstant.BJ_WINDOW_HANDLE_RESULT;
                }
            }
            dto.setServiceHandleWay(handleChannel);
            dto.setServiceHandleResult(handleResult);

            dtoList.add(dto);
        }
        return dtoList;
    }
}